<html>
<head>
	<title>K-FRAMEWORK INSTALLATION</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<script src="../public/default/jscripts/jquery.min.js"></script>
	<script src="../public/default/jscripts/plugins/jquery.validate.js"></script>
	<script language="javascript">
		$(document).ready(function() {
		  // Handler for .ready() called.
			  $("#install").validate({
					rules: {
						dbHost: "required",
						dbUser: "required",
						dbPass: "required",
						dbName: "required",
						template: "required"
					},
					messages: {
						dbHost: "<br/>* คุณยังไม่ได้กรอกข้อมูลนี้",
						dbUser: "<br/>* คุณยังไม่ได้กรอกข้อมูลนี้",
						dbPass: "<br/>* คุณยังไม่ได้กรอกข้อมูลนี้",
						dbName: "<br/>* คุณยังไม่ได้กรอกข้อมูลนี้",
						template: "<br/>* คุณยังไม่ได้กรอกข้อมูลนี้"
					}
			  });
		});

		var tmpHost,tmpUser,tmpPass,tmpName;

		function switchDB()
		{
			if(!$('input[name="dbConnect"]').is(":checked"))
			{
				tmpHost = $('input[name="dbHost"]').val();
				tmpUser = $('input[name="dbUser"]').val();
				tmpPass = $('input[name="dbPass"]').val();
				tmpName = $('input[name="dbName"]').val();
				$('input[name="dbHost"]').val("-").attr('disabled','disabled');
				$('input[name="dbUser"]').val("-").attr('disabled','disabled');
				$('input[name="dbPass"]').val("-").attr('disabled','disabled');
				$('input[name="dbName"]').val("-").attr('disabled','disabled');
			}
			else
			{
				$('input[name="dbHost"]').val(tmpHost).removeAttr('disabled');;
				$('input[name="dbUser"]').val(tmpUser).removeAttr('disabled');;
				$('input[name="dbPass"]').val(tmpPass).removeAttr('disabled');;
				$('input[name="dbName"]').val(tmpName).removeAttr('disabled');;
			}
		}
	</script>
	<style type="text/css"> 
	#install { width: 500px; }
	#install label { width: 250px; }
	#install label.error, #install input.submit {color:#FF0000;font-size:10px;}
	#install { width: 670px; }
	</style>
</head>
<body style="margin-top: 20px;">
<?php
$subDirectory = str_replace($_SERVER['DOCUMENT_ROOT'],"",str_replace("/install","",str_replace("\\","/",getcwd())));
$Host = str_replace("www.","",$_SERVER['HTTP_HOST']).(($subDirectory!="/" && $subDirectory!="")?$subDirectory:"");
$Path = $_SERVER['DOCUMENT_ROOT'].(($subDirectory!="/" && $subDirectory!="")?$subDirectory:"");
require_once '../config.inc.php';
if($_POST)
{
	chmod("../config.inc.php",0777);
	$handle = @fopen("../config.inc.php", "r");

	$data = "";
	if ($handle) {
		while (($buffer = fgets($handle, 4096)) !== false) {

			if(strpos($buffer, "public static \$Host"))
			{
				$data.="\tpublic static \$Host = \"".$Host."\";\n";
			}
			else if(strpos($buffer, "public static \$Path"))
			{
				$data.="\tpublic static \$Path = \"".$Path."\";\n";
			}
			else if(strpos($buffer, "public static \$subFolder"))
			{
				$data.="\tpublic static \$subFolder = \"".$subDirectory."\";\n";
			}
			else if(strpos($buffer, "public static \$template"))
			{
				$data.="\tpublic static \$template = \"".$_POST['template']."\";\n";
			}
			else if(strpos($buffer, "public static \$dbConnect"))
			{
				if($_POST['dbConnect']==true)
				{
					$data.="\tpublic static \$dbConnect = true;\n";
				}
				else
				{
					$data.="\tpublic static \$dbConnect = false;\n";
				}
			}
			else if(strpos($buffer, "public static \$console"))
			{
				if($_POST['console']==true)
				{
					$data.="\tpublic static \$console = true;\n";
				}
				else
				{
					$data.="\tpublic static \$console = false;\n";
				}
			}
			else if(strpos($buffer, "public static \$dbHost"))
			{
				$data.="\tpublic static \$dbHost = \"".$_POST['dbHost']."\";\n";
			}
			else if(strpos($buffer, "public static \$dbUser"))
			{
				$data.="\tpublic static \$dbUser = \"".$_POST['dbUser']."\";\n";
			}
			else if(strpos($buffer, "public static \$dbPass"))
			{
				$data.="\tpublic static \$dbPass = \"".$_POST['dbPass']."\";\n";
			}
			else if(strpos($buffer, "public static \$dbName"))
			{
				$data.="\tpublic static \$dbName = \"".$_POST['dbName']."\";\n";
			}
			else if(strpos($buffer, "public static \$install"))
			{
				$data.="\tpublic static \$install = true;\n";
			}
			else
			{
				$data.=$buffer;
			}
		}
		if (!feof($handle)) {
			echo "Error: unexpected fgets() fail\n";
		}
		fclose($handle);
	}

	file_put_contents("../config.inc.php", $data, LOCK_EX);
	
	echo "<h1>บันทึกข้อมูลเรียบร้อย <a href=\"http://".$Host."\">กดที่นี่ เพื่อไปสู่หน้าหลัก</a></h1>";
}
else
{
?>

<form action="<?php echo  $_SERVER['PHP_SELF'];?>" method="post" id="install" onsubmit="return confirm('ยืนยันข้อมูลถูกต้อง')">
	<h1>ขั้นตอนที่ (1/1) : การติดตั้ง K-FRAMEWORK</h1>
	<hr />
	<table>
		<tr>
			<td colspan="2"><h2>ข้อมูลฐานข้อมูล</h2></td>
		</tr>
		<tr>
			<td style="width:200px;"><input type="checkbox" name="dbConnect" value="true" onchange="switchDB();" <?php echo ((Config::$dbConnect)?"checked":"");?>/> ใช้งานฐานข้อมูล :</td>
			<td></td>
		</tr>
		<tr>
			<td style="width:200px;">โฮสต์ :</td>
			<td><input type="text" name="dbHost" value="<?php echo ((Config::$dbHost)?Config::$dbHost:"-");?>" /></td>
		</tr>
		<tr>
			<td>ชื่อผู้ใช้ :</td>
			<td><input type="text" name="dbUser" value="<?php echo ((Config::$dbUser)?Config::$dbUser:"-");?>" /></td>
		</tr>
		<tr>
			<td>รหัสผ่าน :</td>
			<td><input type="text" name="dbPass" value="<?php echo ((Config::$dbPass)?Config::$dbPass:"-");?>" /></td>
		</tr>
		<tr>
			<td>ชื่อฐานข้อมูล :</td>
			<td><input type="text" name="dbName" value="<?php echo ((Config::$dbName)?Config::$dbName:"-");?>" /></td>
		</tr>
	</table>
	<hr />
	<table>
		<tr>
			<td colspan="2"><h2>ข้อมูลเว็ปไซท์</h2></td>
		</tr>
		<tr>
			<td style="width:200px;">URL หลักของเว็บไซท์ :</td>
			<td><input type="text" name="host" value="<?php echo $Host; ?>" readonly />
				(อัตโนมัติ)</td>
		</tr>
		<tr>
			<td>PATH ของเว็บไซท์ :</td>
			<td><input type="text" name="path" value="<?php echo $Path; ?>" readonly />
				(อัตโนมัติ)</td>
		</tr>
		<tr>
			<td>TEMPLATE มาตรฐาน :</td>
			<td><input type="text" name="template" value="<?php echo ((Config::$template)?Config::$template:"default");?>" /></td>
		</tr>
	</table>
	<hr />
	<table>
		<tr>
			<td colspan="2"><h2>ตัวเลือกทั่วไป</h2></td>
		</tr>
		<tr>
			<td style="width:200px;">แสดงข้อมูลที่ Console :</td>
			<td><input type="checkbox" name="console" value="true" <?php echo ((Config::$console)?"checked":"");?>/></td>
		</tr>
	</table>
	<hr />
	<input type="submit" value="บันทึกข้อมูล" /> <input type="button"
		value="ล้างค่าข้อมูล" />
</form>

<?php } ?>
</body>
</html>
